// src/pages/UserList/components/PasswordModal.tsx
import React, { useState } from 'react';
import { Modal, Form, Input, message } from 'antd';
import { resetPassword } from '@/services/user';

type PasswordModalProps = {
    visible: boolean;
    currentUser?: any; // 根据实际类型调整
    onOk: (values: any) => void;
    onCancel: () => void;
};

const PasswordModal: React.FC<PasswordModalProps> = ({
                                                         visible,
                                                         currentUser,
                                                         onOk,
                                                         onCancel,
                                                     }) => {
    const [form] = Form.useForm();
    const [confirmLoading, setConfirmLoading] = useState(false);

    const handleOk = async () => {
        try {
            const values = await form.validateFields();
            if (!currentUser || !currentUser.id) {
                message.error('用户信息错误');
                return;
            }
            setConfirmLoading(true);

            const response = await resetPassword({ id: currentUser.id, password: values.password });

            if (response.success) {
                message.success(response.message || '密码重置成功');
                onOk(values);
            } else {
                message.error(response.message || '密码重置失败');
            }
        } catch (error) {
            console.error('重置密码失败:', error);
            // message.error('表单验证失败或提交出错');
        } finally {
            setConfirmLoading(false);
        }
    };

    // 自定义密码校验规则
    const validatePassword = (_: any, value: string) => {
        if (!value) {
            return Promise.reject(new Error('请输入新密码!'));
        }
        // 正则：至少8位，包含大小写字母、数字、特殊符号
        if (!/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/.test(value)) {
            return Promise.reject(new Error('密码由8位数字、大小写字母和特殊符号组成!'));
        }
        return Promise.resolve();
    };

    return (
        <Modal
            title="重置密码"
            open={visible}
            onOk={handleOk}
            onCancel={onCancel}
            confirmLoading={confirmLoading}
            maskClosable={false}
            width={500}
        >
            <Form
                form={form}
                layout="vertical"
            >
                <Form.Item
                    name="password"
                    label="新密码"
                    rules={[{ validator: validatePassword }]}
                >
                    <Input.Password placeholder="请输入新密码" />
                </Form.Item>
                <Form.Item
                    name="confirmPassword"
                    label="确认密码"
                    dependencies={['password']}
                    rules={[
                        {
                            required: true,
                            message: '请确认密码!',
                        },
                        ({ getFieldValue }) => ({
                            validator(_, value) {
                                if (!value || getFieldValue('password') === value) {
                                    return Promise.resolve();
                                }
                                return Promise.reject(new Error('两次输入的密码不一致!'));
                            },
                        }),
                    ]}
                >
                    <Input.Password placeholder="请再次输入新密码" />
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default PasswordModal;